package com.situ.car.controller;

import com.situ.car.pojo.entity.Brand;
import com.situ.car.service.IBrandService;
import com.situ.car.service.Impl.BrandServiceImpl;
import com.situ.car.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand")
public class BrandController extends BaseController{
    private IBrandService brandService = new BrandServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BrandController.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectByBrand":
                selectByBrand(req, resp);
                break;
            case "selectByBrandName":
                selectByBrandName(req, resp);
                break;
        }
    }

    private void selectByBrandName(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BrandController.selectByBrandName");
        String brandId = req.getParameter("brandId");
        Brand brand = brandService.selectByBrandName(Integer.parseInt(brandId));
        toJSON(resp, Result.ok(brand));
    }

    private void selectByBrand(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BrandController.selectByBrand");
        List<Brand> list = brandService.selectByBrand();
        toJSON(resp, Result.ok(list));
    }
}
